package com.angel.core.shiro;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.angel.health.SysMenu;
import com.angel.health.SysRole;
import com.angel.health.SysUser;
import com.angel.provider.sys.SysManagerMenuProvider;
import com.angel.provider.sys.SysManagerRoleProvider;
import com.angel.provider.sys.SysManagerUserProvider;


/**
 * 权限检查类
 * 
 * @author ShenHuaJie
 * @version 2016年5月20日 下午3:44:45
 */
public class Realm extends AuthorizingRealm {
	/*@Resource(name="userProvider")
	private SysManagerUserProvider userProvider;
	@Resource(name="roleProvider")
	private SysManagerRoleProvider roleProvider;
	@Resource(name="menuProvider")
	private SysManagerMenuProvider menuProvider;*/
	
	//认证
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

		ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:spring-mvc.xml"); 
		SysManagerUserProvider userProvider = factory.getBean(SysManagerUserProvider.class);
		UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
		String username = usernamePasswordToken.getUsername();
		SysUser user = userProvider.findUserByUsernameAndPassword(username,new String(usernamePasswordToken.getPassword()));
		if(user != null){
			SimpleAuthenticationInfo authcInfo = new SimpleAuthenticationInfo(user,user.getPassword(), this.getName());
            return authcInfo;
		}else{
			throw new UnknownAccountException();
		}
	}
	
	//授权
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {

		ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:spring-mvc.xml"); 
		SysManagerRoleProvider roleProvider = factory.getBean(SysManagerRoleProvider.class);
		SysManagerMenuProvider menuProvider = factory.getBean(SysManagerMenuProvider.class);
		String userId = (String) super.getAvailablePrincipal(principal);
		List<SysRole> roleList = roleProvider.getRolesByUser(Integer.parseInt(userId));
		List<SysMenu> menuList = new ArrayList<SysMenu>();
		List<SysMenu> tmpList = null;
		if(roleList != null && roleList.size() > 0){
			for (SysRole sysRole : roleList) {
				tmpList = menuProvider.getMenuByRole(sysRole.getId());
				menuList.addAll(tmpList);
			}
		}
		List<String> permissions = new ArrayList<String>();
		for (SysMenu sysMenu : menuList) {
			permissions.add(sysMenu.getId().toString());
		}
		SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
		simpleAuthorizationInfo.addStringPermissions(permissions);
		return simpleAuthorizationInfo;
	}
}
